package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BankListItemModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.rxbus.RxBus;
import com.pengcheng.taskapplication.rxbus.RxBusRoute;
import com.pengcheng.taskapplication.rxbus.RxEvent;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class UnbindBank extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView textView;

    @BindView(R.id.tv_bank_name)
    TextView tv_bank_name;

    @BindView(R.id.tv_bank_num)
    TextView tv_bank_num;

    UserPresenterV2 userPresenterV2;

    private String uid;
    private String newtoken;
    private String bindtxsn;



    @Override
    protected int layoutResID() {
        return R.layout.activity_unbind_bank;
    }

    @Override
    protected void initData() {
        textView.setText("解绑银行卡");
        BankListItemModel bankListItemModel=(BankListItemModel)getIntent().getSerializableExtra("model");
        tv_bank_name.setText(bankListItemModel.getBankname());
        bindtxsn=bankListItemModel.getBindtxsn();
        String code=bankListItemModel.getBankaccountnumber();
        tv_bank_num.setText(code.substring(code.length()-4,code.length()));
        userPresenterV2=new UserPresenterV2(this);
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.unbind)){
            //绑定成功，需要跳转
            ToastUtils.showShortToast("解绑成功");
            RxBus.getInstance().post(new RxEvent(RxBusRoute.unbind));
            this.finish();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.img_back,R.id.ll_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_next:
                unbind(newtoken,uid,bindtxsn);
                break;

        }
    }

    public void unbind(String token,String uid,String bindingtxsn){
        userPresenterV2.unbind(token,uid,bindingtxsn, ConstantValue.RequestKey.unbind);
    }
}
